/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package UI.bll;

import BLL.loginRegistration;

import javax.swing.*;
import java.io.File;

import static java.lang.String.valueOf;

/**
 *
 * @author YELIUHUISHI
 */
public class Register extends javax.swing.JFrame {
    Thread timeThread =new Thread();
    /**
     * Creates new form register
     */
    public Register() {
        initComponents();
        init();
    }

    private void init() {
        this.setTitle("注册界面");
        this.setResizable(false);
        uiutlLogin uiutlLogin = new uiutlLogin();
        uiutlLogin.setFrameConter(this);
        uiutlLogin.setFrameTmage(this,"src"+ File.separator+ "UI" +File.separator+"icon"+File.separator+"Register.png");
        timeThread=new Thread(new runnableTime(this.Time));
        timeThread.start();
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        registerName = new javax.swing.JTextField();
        registerPassword = new javax.swing.JPasswordField();
        registerRegister = new javax.swing.JButton();
        registerCancel = new javax.swing.JButton();
        Time = new javax.swing.JLabel();

        Time.setFont(new java.awt.Font("宋体", 0, 18)); // NOI18N
        Time.setForeground(new java.awt.Color(255, 20, 18));
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        jLabel1.setText("注册用户：");
        registerName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerNameActionPerformed(evt);
            }
        });

        jLabel2.setText("注册密码：");
        registerPassword.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerRegisterActionPerformed(evt);
            }
        });

        registerRegister.setText("注册");
        registerRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerRegisterActionPerformed(evt);
            }
        });
        registerCancel.setText("取消");
        registerCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                registerCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(registerRegister)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(registerCancel)
                        .addGap(50, 50, 50))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(Time, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(registerPassword))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(registerName, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(50, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(Time, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(registerName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(registerPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(registerRegister)
                    .addComponent(registerCancel))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void registerNameActionPerformed(java.awt.event.ActionEvent evt) {
        this.registerPassword.requestFocus();
    }


    //注册键动作事件
    private void registerRegisterActionPerformed(java.awt.event.ActionEvent evt) {
        //获取账户，密码
        String userName=this.registerName.getText().trim();
        String userPassword=valueOf(this.registerPassword.getPassword());

        String userNameRegex = "[A-Za-z]{3,20}";
        String userPasswordRegex = "\\w{6,20}";

        //校验
        if(!userName.matches(userNameRegex)){
            JOptionPane.showMessageDialog(this,"用户名不规范（由3个以上英文字母组成）");
            this.registerName.setText("");
            this.registerName.requestFocus();
            return;
        }
        if(!userPassword.matches(userPasswordRegex)){
            JOptionPane.showMessageDialog(this,"密码不满足要求（6到20个任意单词字符）");
            this.registerPassword.setText("");
            this.registerPassword.requestFocus();
            return;
        }
        loginRegistration loginRegistration = new loginRegistration();
        Boolean register = loginRegistration.register(userName, userPassword);
        if(!register){
            JOptionPane.showMessageDialog(this,"注册成功");
            timeThread.stop();
            new Login().setVisible(true);
            this.dispose();
        }else{
            JOptionPane.showMessageDialog(this,"注册失败");
        }
    }
    //取消键动作事件
    private void registerCancelActionPerformed(java.awt.event.ActionEvent evt) {
        timeThread.stop();
        new Login().setVisible(true);
        this.dispose();
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel jLabel1;
    public javax.swing.JLabel jLabel2;
    public javax.swing.JButton registerCancel;
    public javax.swing.JTextField registerName;
    public javax.swing.JPasswordField registerPassword;
    public javax.swing.JButton registerRegister;
    public javax.swing.JLabel Time;
    // End of variables declaration//GEN-END:variables
}
